<?php

namespace App\Http\Controllers\Admin;

use App\Repositories\Admin\AnalysisRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;

class AnalysisController extends Controller
{
    protected $analysisRepository;
    public function __construct(AnalysisRepository $analysisRepository)
    {
        $this->analysisRepository = $analysisRepository;
    }
    /**
     * 重置每日新增用户数
     */
    public function refreshDayUserNum(){
        Redis::set('dayUserNumber',0);
        return json_encode(['code'=>200,'msg'=>'success']);
    }
    /**
     * 获取每日新增用户数
     */
    public function getDayUserNum(){
        $dayUserNumber = 0;
        if(Redis::exists('dayUserNumber')){
            $dayUserNumber = Redis::get('dayUserNumber');
        }else{
            $this->refreshDayUserNum();
            $dayUserNumber = Redis::get('dayUserNumber');
        }
        return json_encode(['dayUserNumber'=>$dayUserNumber]);
    }
    /**
     * 从数据库里获取并更新redis中男女用户数
     */
    public function refreshSexNumFromDB(){

        $male = $this->analysisRepository->getUserBySex(1);
        if($male){
            //将男性用户数量存储在manNumber中
            $maleNum = count($male);
            Redis::set('maleNumber',$maleNum);
        }
        $female = $this->analysisRepository->getUserBySex(2);
        if($female){
            //将女性用户数量存储在femanNumber中
            $femaleNum = count($female);
            Redis::set('femaleNumber',$femaleNum);
        }
        $userNumber = $this->analysisRepository->getUserNumber();
        if($userNumber){
            //将用户数量存储在userNumber中
            Redis::set('userNumber',$userNumber);
        }
       return json_encode(['code'=>200,'msg'=>'success']);
    }
    /**
     * 从Redis中获取男女用户数
     */
    public function getSexNum(){
        $maleNumber = 0;
        $femaleNumber = 0;
        if(Redis::exists('maleNumber')){
            $maleNumber = Redis::get('maleNumber');
        }else{
            $this->refreshSexNumFromDB();
            $maleNumber = Redis::get('maleNumber');
        }
        if(Redis::exists('femaleNumber')){
            $femaleNumber = Redis::get('femaleNumber');
        }else{
            $this->refreshSexNumFromDB();
            $femaleNumber = Redis::get('femaleNumber');
        }
        if(Redis::exists('userNumber')){
            $userNumber = Redis::get('userNumber');
        }else{
            $this->refreshSexNumFromDB();
            $userNumber = Redis::get('userNumber');
        }
//        return 11112222;
        return json_encode(['maleNumber'=>$maleNumber,'femaleNumber'=>$femaleNumber,'userNumber'=>$userNumber]);
    }
    /**
     * 从数据库里获取并更新redis中图片总数
     */
    public function refreshPictureNumFromDB(){
        $pictureNumber = $this->analysisRepository->getPictureNumber();
        if($pictureNumber){
            Redis::set('pictureNumber',$pictureNumber);
        }
        return json_encode(['code'=>200,'msg'=>'success']);
    }

    /**
     * 获取图片总数
     */
    public function getPictureNum(){
        $pictureNumber = 0;
        if(Redis::exists('pictureNumber')){
            $pictureNumber = Redis::get('pictureNumber');
        }else{
            $this->refreshPictureNumFromDB();
            $pictureNumber = Redis::get('pictureNumber');
        }
        return json_encode(['pictureNumber'=>$pictureNumber]);
    }
    /**
     * 重置每日新增图片数
     */
    public function refreshDayPictureNum(){
        Redis::set('dayPictureNumber',0);
        return json_encode(['code'=>200,'msg'=>'success']);
    }
    /**
     * 获取每日新增用户数
     */
    public function getDayPictureNum(){
        $dayPictureNumber = 0;
        if(Redis::exists('dayPictureNumber')){
            $dayPictureNumber = Redis::get('dayPictureNumber');
        }else{
            $this->refreshDayPictureNum();
            $dayPictureNumber = Redis::get('dayPictureNumber');
        }
        return json_encode(['dayPictureNumber'=>$dayPictureNumber]);
    }

    /**
     * 清空所有记录
     */
    public function clearAll(){
        Redis::set('dayPictureNumber',0);//今日新增图片
        Redis::set('dayUserNumber',0);//今日新增用户
        Redis::set('maleNumber',0); //男性用户数
        Redis::set('femaleNumber',0);//女性用户数
        Redis::set('userNumber',0);//用户总数
        Redis::set('pictureNumber',0);//图片总数
        return json_encode(['code'=>200,'msg'=>'success']);
    }
}
